package com.yh.controller;

import com.yh.pojo.User;
import lombok.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Auther: als
 * @Date: 2024/5/20 - 05 - 20 - 10:21
 * @Description: com.yh.controller
 * @version: 1.0
 */
//交给spring容器
@Controller
public class MyController {
    @RequestMapping("/m1")
    @ResponseBody
    public User m1(User u) {
        return u;
    }


    //获取请求头信息
    //1、通过request对象，通过自动注入获取请求头信息
    @RequestMapping("/m2")
    @ResponseBody
    public String m2(HttpServletRequest request) {
        return request.getHeader("User-Agent");
    }

    //2、通过@RequestHeader注解进行获取
    @RequestMapping("/m3")
    @ResponseBody
    public String m3(@RequestHeader(value = "User-Agent", required = false) String UserAgent) {
        return UserAgent;
    }

    //获取请求Cookie数据
    //1、通过通过request对象，获取cookie数据
    @RequestMapping("/m4")
    @ResponseBody
    public Cookie[] m4(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        return cookies;
    }
    //2、使用@CookieValue可以获得指定Cookie的值
    //value：指定cookie的名称
    //required：是否必须携带此cookie
    @RequestMapping("/m5")
    @ResponseBody
    public String m5(@CookieValue String JSESSIONID){
        return JSESSIONID;
    }

    @RequestMapping("/m6")
    @ResponseBody
    public String m6(@CookieValue(value ="JSESSIONID",required = false) String id){
        return id;
    }

    @RequestMapping("/m7")
    @ResponseBody
    public String m7(HttpSession sessione, @SessionAttribute(value = "user",required = false) String user){
         sessione.setAttribute("user","lisi");
        return user;
    }

}
